package com.ht.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.shortlink.project.dao.entity.ShortLinkDO;
import com.ht.shortlink.project.dao.mapper.ShortLinkMapper;
import com.ht.shortlink.project.dto.req.ShortLinkRecyclePageReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkRecycleRecoverReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkRecycleRemoveReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkRecycleSaveReqDTO;
import com.ht.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.ht.shortlink.project.dto.resp.ShortLinkRecyclePageRespDTO;
import com.ht.shortlink.project.service.IRecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.ht.shortlink.project.common.constant.RedisCacheConstant.SHORT_LINK_GOTO_IS_NULL_KEY;
import static com.ht.shortlink.project.common.constant.RedisCacheConstant.SHORT_LINK_GOTO_KEY;


/**
 *  回收站接口实现层
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLinkDO> implements IRecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void save2Bin(ShortLinkRecycleSaveReqDTO requestParam) {
        lambdaUpdate().eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 0)
                .eq(ShortLinkDO::getDelFlag, 0)
                .set(ShortLinkDO::getEnableStatus, 1)
                .update();
        stringRedisTemplate.delete(String.format(SHORT_LINK_GOTO_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public IPage<ShortLinkPageRespDTO> queryPage(ShortLinkRecyclePageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .in(ShortLinkDO::getGid, requestParam.getGidList())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getDelFlag, 0)
                .orderByDesc(ShortLinkDO::getUpdateTime);
        IPage<ShortLinkDO> resultPage = baseMapper.selectPage(requestParam, queryWrapper);
        return resultPage.convert(each -> {
            ShortLinkPageRespDTO result = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            result.setDomain("http://" + result.getDomain());
            return result;
        });
    }

    @Override
    public void recycleRecover(ShortLinkRecycleRecoverReqDTO requestParam) {
        lambdaUpdate().eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getDelFlag, 0)
                .set(ShortLinkDO::getEnableStatus, 0)
                .update();
        stringRedisTemplate.delete(String.format(SHORT_LINK_GOTO_IS_NULL_KEY + requestParam.getFullShortUrl()));
    }

    @Override
    public void recycleRemove(ShortLinkRecycleRemoveReqDTO requestParam) {
        lambdaUpdate().eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getDelTime, 0)
                .set(ShortLinkDO::getDelFlag, 1)
                .set(ShortLinkDO::getDelTime, System.currentTimeMillis())
                .update();
    }
}
